package com.manager.service.impl;

import com.manager.bean.PageInfo;
import com.manager.bean.StudentAccess;
import com.manager.bean.StudentAccessExample;
import com.manager.mapper.StudentAccessMapper;
import com.manager.service.StudentAccessService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Studentaccess)表服务实现类
 *
 * @author makejava
 * @since 2021-03-02 11:00:26
 */
@Service("studentaccessService")
public class StudentAccessServiceImpl implements StudentAccessService {
    @Resource
    private StudentAccessMapper studentaccessDao;


    @Override
    public PageInfo<StudentAccess> findPageInfo(Integer studentId, Integer dormitoryId, Integer pageIndex, Integer pageSize) {
        return null;
    }

    @Override
    public StudentAccess queryById(Integer id) {
        StudentAccessExample studentAccessExample = new StudentAccessExample();
        StudentAccessExample.Criteria criteria = studentAccessExample.createCriteria();
        criteria.andStudentIdEqualTo(id);
        studentAccessExample.setOrderByClause("id desc");
        return this.studentaccessDao.selectByExample(studentAccessExample).get(0);
    }

    @Override
    public StudentAccess queryByStuIdDESC(Integer studentId) {
        StudentAccessExample studentAccessExample = new StudentAccessExample();
        StudentAccessExample.Criteria criteria = studentAccessExample.createCriteria();
        criteria.andStudentIdEqualTo(studentId);
        studentAccessExample.setOrderByClause("id DESC");
        List<StudentAccess> studentAccesses = studentaccessDao.selectByExample(studentAccessExample);
        if(studentAccesses.size()==0){
            return null;
        }
        else{
            return studentAccesses.get(0);
        }
    }


    @Override
    public List<StudentAccess> getAll(){
        StudentAccessExample studentAccessExample = new StudentAccessExample();
        StudentAccessExample.Criteria criteria = studentAccessExample.createCriteria();
        return studentaccessDao.selectByExample(studentAccessExample);
    }

    /**
     * 新增数据
     *
     * @param studentaccess 实例对象
     * @return 实例对象
     */
    @Override
    public StudentAccess insert(StudentAccess studentaccess) {
        this.studentaccessDao.insert(studentaccess);
        return studentaccess;
    }

    /**
     * 修改数据
     *
     * @param studentaccess 实例对象
     * @return 实例对象
     */
    @Override
    public StudentAccess update(StudentAccess studentaccess) {
        this.studentaccessDao.updateByPrimaryKey(studentaccess);
        return this.queryById(studentaccess.getStudentId());
    }

    /**
     * 通过主键删除数据
     *
     * @param studentId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer studentId) {
        return this.studentaccessDao.deleteByPrimaryKey(studentId) > 0;
    }
}
